local Piece = require "packages.chess-games.nchess.core.piece"

local Bishop = Piece:subclass("NChess.Bishop")

function Bishop:initialize(id)
  Piece.initialize(self, id)

  self.name = 'bishop'
  self.whiteStr = '象'
  self.blackStr = '象'
end

---@param board integer[][]
---@param fromX integer
---@param fromY integer
---@param x integer
---@param y integer
function Bishop:canMoveTo(board, fromX, fromY, x, y)
  local dx = x - fromX
  local dy = y - fromY

  if (dx ~= dy) and (dx + dy ~= 0) then
    return false
  end

  --斜行阻挡
  local steps = { math.floor(dy / math.abs(dy)), math.floor(dx / math.abs(dx)) }
  for i = 1, math.abs(dx) - 1 do
    if board[fromY + (i * steps[1])][fromX + (i * steps[2])] ~= 0 then
      return false
    end
  end

  -- 不过河
  local myColor = self:getColor()

  -- 目标位置若有己方棋子，不可走
  local targetId = board[y][x]
  if targetId ~= 0 then
    local targetColor = (targetId > 0) and "white" or "black"
    if myColor == targetColor then
      return false
    end
  end

  return true
end

return Bishop

